package pers.cloud.cos.qiniu;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import pers.cloud.cos.qiniu.config.QiniuCOSConfig;
import pers.cloud.cos.throwable.MyCloudCOSException;

import java.io.File;


public class QiniuCOS {

    private QiniuCOSConfig qiniuCOSConfig;

    public QiniuCOS(QiniuCOSConfig qiniuCOSConfig){
        this.qiniuCOSConfig = qiniuCOSConfig;
    }

    /**
     * 上传本地文件
     * @param filePath 文件
     * @param key 文件名称
     * @param override 是否覆盖同名同位置文件
     * @return
     */
    public String uploud(String filePath, String key, boolean override) throws MyCloudCOSException {

        // 构建带指定Zone对象的配置类
        Configuration cfg = new Configuration(qiniuCOSConfig.getZone());

        // 生成凭证准备上传
        UploadManager uploadManager = new UploadManager(cfg);

        Auth auth = Auth.create(qiniuCOSConfig.getAccessKey(),qiniuCOSConfig.getSecretKey());

        String upToken;
        if(override){
            upToken = auth.uploadToken(qiniuCOSConfig.getBucketName(),key);
        } else {
            upToken = auth.uploadToken(qiniuCOSConfig.getBucketName());
        }
        try {
            Response response = uploadManager.put(filePath, key,upToken);
            // 解析上传成功的结果
            DefaultPutRet defaultPutRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(defaultPutRet.hash);
            return defaultPutRet.key;
        } catch (QiniuException e) {
//            Response response = e.response;
            e.printStackTrace();
            throw new MyCloudCOSException(e.response.toString());
//            throw new MyCloudCOSException(response.bodyString());
        }

    }

        public static void main(String[] args) throws MyCloudCOSException {
////
////        System.out.println(upToken);
//        // 构建带指定Zone对象的配置类
//        Configuration cfg = new Configuration(Zone.zone0());
//
//        // 生成凭证准备上传
//        UploadManager uploadManager = new UploadManager(cfg);
//
//        String localFilePath = "C:\\Users\\LYM\\Pictures\\Saved Pictures\\微信图片_20190313152913.jpg";
//
//        String bucket = "mashangjiuban";
//
//        Auth auth = Auth.create("gRJmNz_MO6WvvxRXyKX3oXEMeAOiTHzMrlOJBQLo","06FNoCQuZ20sz6kEYtiQgXHbLtUJQwUWSyd4haaK");
//        String upToken = auth.uploadToken("mashangjiuban");
//
//        try {
//            Response response = uploadManager.put(localFilePath, null, upToken);
//            //解析上传成功的结果
//            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//            System.out.println(putRet.key);
//            System.out.println(putRet.hash);
//        } catch (QiniuException e) {
//            Response r = e.response;
//            r.close();
//            System.err.println(r.toString());
//            try {
//                System.err.println(r.bodyString());
//            } catch (QiniuException e1) {
//                e1.printStackTrace();
//            }
//        }
//        System.out.println("+++++");

//            QiniuCOSConfig qiniuCOSConfig = new QiniuCOSConfig.Builder()
            QiniuCOSConfig qiniuCOSConfig = new QiniuCOSConfig.Builder()
                    .addSecretKey("06FNoCQuZ20sz6kEYtiQgXHbLtUJQwUWSyd4haaK")
                    .addAccessKey("gRJmNz_MO6WvvxRXyKX3oXEMeAOiTHzMrlOJBQLo")
                    .addBucketName("blog")
                    .addZone(Zone.zone0()).build();
            QiniuCOS qiniuCOS = new QiniuCOS(qiniuCOSConfig);

            System.out.println(qiniuCOS.uploud("C:\\Users\\LYM\\Pictures\\全国同城货运配送_3A级城市物流配送中心-蚂蚁物流官网_files\\201903061527251780.jpg",
                    "201807031027393455.jpg",true));

    }

}

